<?php
declare (strict_types=1);

namespace app\admin\controller\study;

use app\common\model\StudyStudentClass;
use app\common\model\StudyStudentList;
use app\common\model\UsersIndex;
use think\facade\Request;
use app\common\service\StudyStudentList as S;
use app\common\model\StudyStudentList as M;

class StudentList extends \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];

    // 列表
    public function index()
    {
        return $this->getAuto($this->fetch(), M::getList());
    }

    // 添加
    public function add()
    {
        $admin = session('admin');
        $cla = StudyStudentClass::where('school_id', $admin['school_id'])->select();
        return $this->getAuto($this->fetch('', ['cla' => $cla]), S::goAdd(Request::post()));
    }

    // 编辑
    public function edit($id)
    {
        $admin = session('admin');
        $cla = StudyStudentClass::where('school_id', $admin['school_id'])->select();
        return $this->getAuto($this->fetch('', ['model' => M::find($id), 'cla' => $cla]), S::goEdit(Request::post(), $id));
    }

    // 状态
    public function status($id)
    {
        return $this->getJson(S::goStatus(Request::post('status'), $id));
    }

    // 删除
    public function remove($id)
    {
        return $this->getJson(S::goRemove($id));
    }

    // 批量删除
    public function batchRemove()
    {
        return $this->getJson(S::goBatchRemove(Request::post('ids')));
    }

    // 回收站
    public function recycle()
    {
        return $this->getAuto($this->fetch(), S::goRecycle());
    }

    /**
     * 学生信息
     * @param $id
     * @return mixed|object|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function studentInfo($id)
    {
        $child = StudyStudentList::where('id', $id)->find();
        $child['class'] = StudyStudentClass::where('id', $child['class_id'])->find();
        $child['parent'] = UsersIndex::where('id', $child['parent_id'])->find();
        return $this->getAuto($this->fetch('', ['child' => $child]), M::getList());
    }

    public function action()
    {
        $id = input('param.id');
        return $this->getAuto($this->fetch('', ['id' => $id]), M::getList());
    }

    public function attendance()
    {
        $id = input('param.id');
        return $this->getAuto($this->fetch('', ['id' => $id]), M::getAttendance());
    }

    public function student_body()
    {
        $id = input('param.id');
        return $this->getAuto($this->fetch('', ['id' => $id]), M::studentBody());
    }

    public function vaccines()
    {
        $id = input('param.id');
        return $this->getAuto($this->fetch('', ['id' => $id]), M::studentVaccines());
    }

    public function report()
    {
        $id = input('param.id');
        return $this->getAuto($this->fetch('', ['id' => $id]), M::studentReport());
    }

}
